<?php
include_once 'cron.inc.php';
$selfname = 'taskcron/like.php';
$infoid = 301;
$server = intval($_GET['server']) ? $_GET['server'] : null;
if ($server == null) {
    exit('提交参数错误！');
}
if (isset($_GET['multi'])) {
    $start = intval($_GET['start']);
    $num = intval($_GET['num']);
    $sql = "select * from zy_qqjob where server=:server and cookiezt=0 type='zan' and nexttime<={$time} order by nexttime asc limit {$start},{$num}";
    $pdoStmt = $pdo->prepare($sql);
    if (true == $pdoStmt) {
        $rs = $pdoStmt->execute(['server' => daddslashes($server)]);
    }
} elseif ($configs['runmodol'] == 0) {
    $sql = "select * from zy_qqjob where server=:server and cookiezt = 0 and type='zan' and nexttime<={$time}";
    $pdoStmt = $pdo->prepare($sql);
    if (true == $pdoStmt) {
        $rs = $pdoStmt->execute(['server' => daddslashes($server)]);
    }
    $nump = $pdoStmt->fetchAll();
    $xz = ceil(count($nump) / $size);
    if ($xz > 1) {
        for ($i = 0; $i <= $xz; $i++) {
            $start = $i * $size;
            curl_run($siteurl . $selfname . '?cronkey=' . $_GET['cronkey'] . '&multi=on&start=' . $start . '&num=' . $size);
        }
        exit("Successful opening of {$xz} threads!");
    } else {
        $sql = "select * from zy_qqjob where  server=:server and cookiezt=0 and nexttime<={$time} and type='zan' order by nexttime asc";
        $pdoStmt = $pdo->prepare($sql);
        if (true == $pdoStmt) {
            $rs = $pdoStmt->execute(['server' => daddslashes($server)]);
        }
    }
} else {
    $sql = "select * from zy_qqjob where server=:server and  cookiezt=0 and nexttime<={$time } and type='zan'";
    $pdoStmt = $pdo->prepare($sql);
    if (true == $pdoStmt) {
        $rs = $pdoStmt->execute(['server' => daddslashes($server)]);
    }
    $nump = $pdoStmt->fetchAll();
    $xz = ceil(count($nump) / $size);
    $shu = $pdo->query("SELECT * FROM zy_info WHERE sysid='{$infoid}' limit 1");
    $shu = $shu->fetch();
    $shu = $shu['times'];
    $up_shu = $shu + 1;
    if ($shu >= $xz) {
        $pdo->exec("update zy_info set times='1' where sysid='{$infoid}'");
        $shu = 0;
    } else {
        $pdo->exec("update zy_info set times='" . $up_shu . "' where sysid='{$infoid}'");
    }
    $shu = $shu * $size;
    $sql = "select * from zy_qqjob where server=:server and type='zan'  and cookiezt=0 and nexttime<={$time} order by nexttime asc limit {$shu},{$size}";
    $pdoStmt = $pdo->prepare($sql);
    if (true == $pdoStmt) {
        $rs = $pdoStmt->execute(['server' => daddslashes($server)]);
    }
}
while ($row = $pdoStmt->fetch()) {
    if ($row['nexttime'] > $time) {
        continue;
    }
    $id = $row['jobid'];
    if ($row['start'] == 0) {
        continue;
    }
    $url = qqtaskApi($row['type'], $row['qid']);
    $curl = curl_init();
    $urlarr = parse_url($url);
    if ($configs['local_cron'] == 1 && $urlarr['host'] == $_SERVER['HTTP_HOST']) {
        $url = str_replace('http://' . $_SERVER['HTTP_HOST'] . '/', 'http://127.0.0.1:80/', $url);
        $url = str_replace('https://' . $_SERVER['HTTP_HOST'] . '/', 'https://127.0.0.1:443/', $url);
        $url .= '&localcron=1';
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host: ' . $_SERVER['HTTP_HOST']));
    }
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($curl, CURLOPT_TIMEOUT, 1);
    curl_setopt($curl, CURLOPT_NOBODY, 1);
    curl_setopt($curl, CURLOPT_NOSIGNAL, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36');
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_exec($curl);
    curl_close($curl);
    $sleep2 = $configs['sleep2'] < 10 ? 10 : $configs['sleep2'];
    $interval = $sleep2;
    $nexttime = $time + $interval;
    $pdo->exec("update `zy_qqjob` set `times`=`times`+1,`lasttime`='{$date2}',`nexttime`='{$nexttime}' where `jobid`='{$id}'");
}
$pdo->exec("update `zy_info` set `last`='{$date}' where sysid='{$infoid}'");
$pdo->exec("update `zy_info` set `times`=`times`+1,`last`='{$date}' where sysid='0'");
$pdo = null;
echo "<head><title>success in run</title></head>";
echo $date;